package com.edu.koudingmusic.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.andraskindler.quickscroll.Scrollable;
import com.edu.koudingmusic.R;
import com.edu.koudingmusic.entities.Mp3Info;
import com.edu.koudingmusic.tools.MediaUtils;

import java.util.ArrayList;

/**
 * Created by Ming on 2016/2/23.
 * email 1715944993@qq.com
 */
public class MyMusicListAdapter extends BaseAdapter implements Scrollable {

    private Context mContext;
    private ArrayList<Mp3Info> mList;

    public MyMusicListAdapter(Context context, ArrayList<Mp3Info> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void setList(ArrayList<Mp3Info> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.iem_music_list, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        Mp3Info mp3Info = mList.get(position);
        viewHolder.mMusicTitle.setText(mp3Info.getTitle());
        viewHolder.mMusicSinger.setText(mp3Info.getArtist());
        viewHolder.mMusicDuration.setText(MediaUtils.formatTime(mp3Info.getDuration()));
        return convertView;
    }

    @Override
    public String getIndicatorForPosition(int childposition, int groupposition) {
        return Character.toString(mList.get(childposition).getTitle().charAt(0));
    }

    @Override
    public int getScrollPosition(int childposition, int groupposition) {
        return childposition;
    }

    private static class ViewHolder {

        private TextView mMusicTitle;
        private TextView mMusicSinger;
        private TextView mMusicDuration;

        public ViewHolder(View itemView) {
            mMusicTitle = (TextView) itemView.findViewById(R.id.item_music_title);
            mMusicSinger = (TextView) itemView.findViewById(R.id.item_music_singer);
            mMusicDuration = (TextView) itemView.findViewById(R.id.item_music_duration);
        }

    }
}
